#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
//全局变量
int ball_x,ball_y;//小球的坐标 
int high,width;//游戏画面尺寸 
int ball_vx,ball_vy;//小球的速度 
int position_x,position_y;//挡板的中心坐标 
int radius;//挡板的半径
int left,right;//挡板左右边界 
int ball_number;//得分 
int block_x,block_y;//砖块的位置 

void gotoxy(int x,int y)//光标移动到（x，y） 位置 
{
	HANDLE handle = GetStdHandle (STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition (handle, pos);
} 





void hide()//隐藏光标 
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info);
} 





int starup()//游戏初始化 
{
	high=18;
	width=25;
	ball_x=1;
	ball_y=width/2;
	ball_vx=1;
	ball_vy=1;
	
	position_x=high;
	position_y=width/2;
	radius=5; 
	left=position_y-radius;
	right=position_y+radius;
	ball_number=0; 
	block_x=2;
	block_y=width/2;
	hide() ;
}





void show()//游戏显示
{	
	gotoxy(0,0);//清屏 
	int i,j;
	for(i=0;i<=high+1;i++)
	{
		for(j=0;j<=width;j++)
		{
			if((i==ball_x)&&(j==ball_y))
				printf("*");//显示小球 
			else if(j==width)
			{
				printf("|");//输出右边界 
			}
			else if(i==high+1)//输出下边界 
			{
				printf("-");
			} 
			else if((i==high)&&(j>=left)&&(j<=right))
			{
				printf("*");//输出一个挡板 
			}
			else if((i==block_x)&&(j==block_y))
			{
					printf("#");
			}
			else
				printf(" ");//显示空格 
		}
		printf("\n");
	}
	printf("碰撞次数为：%d",ball_number);
	printf("\n");
}





int updataWithoutInput()//用户无关设置
{	

	if((ball_x==block_x)&&(ball_y==block_y))
		{
			printf("You Win!!!");
		}


	if(ball_x==high)
	{
		if(ball_y>=left&&ball_y<=right)
		{
			ball_number++;
		}
		else
		{
			printf("Game Over!");
			exit(0);
		}
		
	}




	//根据速度更新小球位置 
	ball_x=ball_x+ball_vx; 
	ball_y=ball_y+ball_vy; 
	//碰到边界后改变小球方向
	if((ball_x==0)||(ball_x==high))
		ball_vx=-ball_vx;
	if((ball_y==0)||(ball_y==width))
		ball_vy=-ball_vy;
	Sleep(50);
}	





int updataWithInput()//用户有关设置
{
	char input;
	if(kbhit())//当按下键时执行
	{
		input=getch();
		if(input=='a')
		{
			position_y--;
			left=position_y-radius;
			right=position_y+radius;
			
			
		}
		if(input=='d')
		{
			position_y++;
			left=position_y-radius;
			right=position_y+radius;

			
		}
			

	} 
}





int main()
{
	hide(); 
	starup();//数据初始化
	while(1)//游戏循环 
	{
		show();//游戏显示 
		updataWithoutInput();//用户无关设置 
		updataWithInput(); //用户有关设置 
	} 
	return 0; 
} 
